package com.futures.investm.common;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.futures.investm.R;
import com.futures.investm.constant.BussConstant;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.List;

import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener;
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager;
import tech.com.commoncore.base.BaseTitleActivity;


public class ProtocolActivity extends BaseTitleActivity {


    private WebView mWebview;

    private StatusLayoutManager mStatusManager;

    @Override
    public int getContentLayout() {
        return R.layout.activity_protocol;
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("用户注册协议");
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mWebview = findViewById(R.id.webview);

        WebSettings settings = mWebview.getSettings();
        settings.setJavaScriptEnabled(true);

        initStatusLayoutManager();

        mStatusManager.showLoadingLayout();
    }

    private void initStatusLayoutManager() {
        StatusLayoutManager.Builder builder = new StatusLayoutManager.Builder(mWebview)
                .setDefaultLayoutsBackgroundColor(android.R.color.transparent)
                .setDefaultEmptyText("")
                .setDefaultLoadingText("加载中...")
                .setDefaultErrorText("获取协议失败")
                .setOnStatusChildClickListener(new OnStatusChildClickListener() {
                    @Override
                    public void onEmptyChildClick(View view) {

                    }

                    @Override
                    public void onErrorChildClick(View view) {
                        //默认点击刷新
                        mStatusManager.showLoadingLayout();
                        loadData();
                    }

                    @Override
                    public void onCustomerChildClick(View view) {

                    }
                });
        mStatusManager = builder.build();
    }

    @Override
    public void loadData() {
        getProtocol();
    }

    //获取协议
    private void getProtocol() {
        AVQuery<AVObject> query = new AVQuery<>(BussConstant.TABLE_PROTOCOL);
        query.setLimit(1);
        query.orderByDescending("createdAt");
        query.findInBackground(new FindCallback<AVObject>() {

            @Override
            public void done(List<AVObject> list, AVException e) {
                // list 是一个 AVObject 的 List
                // list 指的就是所有包含当前 tag 的 TodoFolder
                if (e == null) {
                    if (list != null) {
                        AVFile file = (AVFile) list.get(0).get(BussConstant.KEY_FILE);
                        mWebview.loadUrl(file.getUrl());
                    }
                    mStatusManager.showSuccessLayout();
                } else {
                    mStatusManager.showErrorLayout();
                }
            }
        });
    }


}
